package org.example.day07;
/**
 * 第三章节3.1.11: 方法notify()不会立即释放锁
 *      ①方法wait立即释放锁，当被唤醒后，不会立即执行，而是等notify的线程释放了锁，才会开始执行
 *      ②方法notify方法不会立即释放锁，唤醒wait线程后，继续自己的执行，直到释放锁.
 */
public class MyThread9 {
    private Object lock = new Object();
    public void waitMethod(){
        try {
            synchronized (lock){
                System.out.println("begin wait ThreadName = " + Thread.currentThread().getName() + " time = " + System.currentTimeMillis());
                lock.wait();
                System.out.println("end wait ThreadName = " + Thread.currentThread().getName() + " time = " + System.currentTimeMillis());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void notifyMethod(){
        try {
            synchronized (lock){
                System.out.println("begin notify ThreadName = " + Thread.currentThread().getName() + " time = " + System.currentTimeMillis());
                lock.notify();
                Thread.sleep(5000);
                System.out.println("end notify ThreadName = " + Thread.currentThread().getName() + " time = " + System.currentTimeMillis());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyThread9 m9 = new MyThread9();
        Thread9A ta = new Thread9A(m9);
        ta.start();
        Thread.sleep(100);
        Thread9B tb = new Thread9B(m9);
        tb.start();
    }
}

class Thread9A extends Thread{
    private MyThread9 m9;

    public Thread9A(MyThread9 m9) {
        this.m9 = m9;
    }

    @Override
    public void run() {
        m9.waitMethod();
    }
}

class Thread9B extends Thread{
    private MyThread9 m9;

    public Thread9B(MyThread9 m9) {
        this.m9 = m9;
    }

    @Override
    public void run() {
        m9.notifyMethod();
    }
}


